package com.simplemonkeytest;

import com.jme.app.SimpleGame;
import com.jme.image.Texture;
import com.jme.input.MouseInput;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.simplemonkey.Container;
import com.simplemonkey.ListeningContainer;
import com.simplemonkey.layout.BorderLayout;
import com.simplemonkey.layout.BorderLayoutData;
import com.simplemonkey.widgets.SimpleButton;
import com.simplemonkey.widgets.SimpleButtonListener;

public class Tutorial2 extends SimpleGame {
	private Node hudNode;

	public static void main(String[] args) {
		Tutorial2 app = new Tutorial2();
	    app.setConfigShowMode(ConfigShowMode.AlwaysShow);
		app.start();
	}

	protected void simpleInitGame() {
		input.setEnabled(false);//we don't want the cursor moving the camera, because we are using it for the gui.
		MouseInput.get().setCursorVisible(true);//show cursor
		super.graphNode.setCullHint(Spatial.CullHint.Always);
		hudNode = new Node();
		hudNode.setCullHint(Spatial.CullHint.Never);
		hudNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		hudNode.setLightCombineMode(Spatial.LightCombineMode.Off);

		ZBufferState bufferState = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
		bufferState.setEnabled( true );
		bufferState.setFunction( ZBufferState.TestFunction.Always );//if CF_ALWAYS is selected, then ZOrder will be used to determine which pixel to display.
		bufferState.setWritable(true);
		hudNode.setRenderState(bufferState);

		//Set the hudNode to use transparency
		BlendState blendState = display.getRenderer().createBlendState();
		blendState.setBlendEnabled(true);
		blendState.setSourceFunctionAlpha(BlendState.SourceFunction.SourceAlpha);
		blendState.setDestinationFunctionAlpha(BlendState.DestinationFunction.OneMinusSourceAlpha);
		blendState.setBlendEquation(BlendState.BlendEquation.Subtract);
		blendState.setTestEnabled(false);
		blendState.setEnabled(true);
		hudNode.setRenderState(blendState);
		hudNode.updateRenderState();

		ListeningContainer mainContainer = new ListeningContainer();
		MouseInput.get().addListener(mainContainer);
		hudNode.attachChild(mainContainer.getSpatial());//attach the spatial in this container to the given node
		//We also want to set out container to fill the whole screen.
		mainContainer.setSize(DisplaySystem.getDisplaySystem().getWidth(),DisplaySystem.getDisplaySystem().getHeight());
		mainContainer.setLayoutManager(new BorderLayout());
		mainContainer.setSize(DisplaySystem.getDisplaySystem().getWidth(),DisplaySystem.getDisplaySystem().getHeight());

		Container northContainer = new Container();
		northContainer.setLayoutData(BorderLayoutData.NORTH);
		northContainer.setLayoutManager(new BorderLayout());
		mainContainer.addWidget(northContainer);

		
		Texture up = TextureManager.loadTexture(getClass().getClassLoader().getResource("com/simplemonkey/data/samplebutton/capsuleButtonUp.png"),Texture.MinificationFilter.Trilinear,Texture.MagnificationFilter.Bilinear);
		Texture down = TextureManager.loadTexture(getClass().getClassLoader().getResource("com/simplemonkey/data/samplebutton/capsuleButtonDown.png"),Texture.MinificationFilter.Trilinear,Texture.MagnificationFilter.Bilinear);
		Texture hover = TextureManager.loadTexture(getClass().getClassLoader().getResource("com/simplemonkey/data/samplebutton/capsuleButtonHover.png"),Texture.MinificationFilter.Trilinear,Texture.MagnificationFilter.Bilinear);

		SimpleButton exit = new SimpleButton("Exit",up,down,hover);
		exit.setLayoutData(BorderLayoutData.EAST);
		//The default size of simple buttons is the size of the textures applied to them.
		//I don't want them to shrink below that, so I will set shrinkable to be false.
		exit.setShrinkable(false);
		northContainer.addWidget(exit);

		exit.addButtonListener(new SimpleButtonListener()
		{
			public void buttonPressed() {
				System.exit(0);
			}
		});
        //Finally, layout everything
		mainContainer.layout();
	}
	protected void simpleRender() {
		//we want to render the hudNode by itself here.
		display.getRenderer().draw(hudNode);
	}
}




